#!/bin/bash

function checkcmd(){
    command -v $1 >/dev/null 2>&1 || { echo >&2 "please check cmd:$1 is available!"; exit -1; }
    type $1 >/dev/null 2>&1 || { echo >&2 "please check cmd:$1 is available!"; exit -1; }
    hash $1 2>/dev/null || { echo >&2 "please check cmd:$1 is available!"; exit -1; }
}


#环境检查
checkcmd curl
checkcmd zip
checkcmd getopts
checkcmd npm
checkcmd gulp


#获取参数
while getopts m:p:n:g: opt
do  
    case $opt in
        m)      
            model=$OPTARG
            ;;
        p)      
            uploadPath=$OPTARG
            ;;
        n)      
            isnpm=$OPTARG
            ;;
        g)      
            isgulp=$OPTARG
            ;;
    esac    
done




root=~
username=`cat "$root"/user`
password=`cat "$root"/pwd`
folderPath="./styles/ ./images/ ./scripts"
productName="limitUp"
distFolderRoot="/var/www/html/"
distFolderMiddle="release/"
distFolderName="vaserviece/"$productName"/"
cdnPath=$distFolderRoot"js/m/"$productName


#是否需要安装npm
if [ $isnpm == 'true' ]
then
    #npm install
    if [ ! -d "node_modules" ]
    then
        cmd="ln -s /home/sjcg/nodeModules/gulpModules/node_modules node_modules"
        echo "$cmd"
        ret=$($cmd)
    fi
fi

#mkdir -p $distFolderRoot

case $isgulp in 
    0)
        gulp
        
        if [ $? -eq 0 ]
        then
            echo 'gulp is ok!'
            
            #cdn
            cmd="mkdir $cdnPath"
            echo "$cmd"
            ret=$($cmd)
            cd ./dist/
            for folder in $folderPath
            do
                echo $folder
                cmd="cp -rf $folder $cdnPath"
                echo "$cmd"
                ret=$($cmd)
            done
            #cdnend
                
            exit 0
        else
            echo 'gulp is failed!'
            exit 1
        fi
    ;;
    1)
        gulp build
        
        if [ $? -eq 0 ]
        then
            echo 'gulp build is ok!'
            
            #cdn
            cmd="mkdir $cdnPath"
            echo "$cmd"
            ret=$($cmd)
            cd ./dist/
            for folder in $folderPath
            do
                echo $folder
                cmd="cp -rf $folder $cdnPath"
                echo "$cmd"
                ret=$($cmd)
            done
            #cdnend
            
            exit 0
        else
            echo 'gulp build is failed!'
            exit 1
        fi
    ;;
    2)
        mkdir -p $distFolderRoot$distFolderMiddle$distFolderName
        echo 'mkdir '$distFolderRoot$distFolderMiddle$distFolderName' done!'
        
        gulp release
        
        if [ $? -eq 0 ]
        then
            echo 'gulp release is ok!'
            
            cp -rf dist/* $distFolderRoot$distFolderMiddle$distFolderName
            echo 'cp -rf dist/* '$distFolderRoot$distFolderMiddle$distFolderName' done!'
        else
            echo 'gulp release is failed!'
            exit 1
        fi
    ;;
esac

if [ $? -eq 0 ]
then
        
    cd ./dist/
                    
    if [ "$uploadPath"x == ''x ] ;then
        echo 'skip upload img,css,js to cdn step!'
        exit 0
    fi
    
    cmd="curl admin.thsi.cn/index.php?controller=login&action=in -d user_name=$username&password=$password -sig"
    #echo "$cmd"
    ret=$($cmd)
    #echo "$ret"
    
    msg=$(echo "$ret" | grep success)
    if [ "$msg"x != '{success:true}'x ] ;then
        echo "$ret"
        exit 3
    fi
    
    cookie=$(echo "$ret" | grep PHPSESSID | awk '{print $2}')
    if [ "$cookie"x == ''x ] ;then
        echo "shell script error! please connect dev"
        exit 4
    fi
    
    echo "upload path http://s.thsi.cn/js/m/$uploadPath  file:"

    if [ "$model"x == 'true'x ]
    then
        #批量上传
        for folder in $folderPath
        do
            zip -r upload.zip $folder
            cmd="curl admin.thsi.cn/index.php?controller=file&action=upload -F path=/html/s/js/m/$uploadPath -F zipFlag=true -F file=@./upload.zip -H Cookie:$cookie -sg"
            #echo "$cmd"
            ret=$($cmd);
            echo "$ret"
            
            rm ./upload.zip
        done
    
    else
        #单个文件上传
        for folder in $folderPath
        do
            filelist=$(ls $folder)
            echo $folder
            for file in $filelist
            do
                cmd="curl admin.thsi.cn/index.php?controller=file&action=upload -F path=/html/s/js/m/$uploadPath/$folder  -F file=@$folder/$file -H Cookie:$cookie -sg" 
                #echo $cmd;
                ret=$($cmd);
                echo "$file : $ret"
            done
        done
    fi        
    
else
    exit 1;
fi
